#!/bin/bash

# 确保脚本以 root 权限运行
if [ "$(id -u)" != "0" ]; then
   echo "该脚本必须以 root 权限运行" 1>&2
   exit 1
fi

# 提示用户确认操作
read -p "Are you sure you want to proceed? (Yes/No) " -n 1 -r
echo    # (optional) 打印一个新行

if [[ $REPLY =~ ^[Yy]$ ]]
then
    echo "Operation confirmed. Proceeding with the script..."

    # 停止并禁用firewalld
    echo "Stopping and disabling firewalld..."
    systemctl stop firewalld
    systemctl disable firewalld
    echo "firewalld stopped and disabled."

    # 关闭SELinux
    echo "Setting SELinux to permissive mode..."
    setenforce 0
    sed -i 's/^SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
    echo "SELinux has been disabled."

    # 修改 CentOS 仓库配置文件，注释掉 mirrorlist 行
    echo "Modifying CentOS repository configuration..."
    sudo sed -i -e "s|^mirrorlist=|#mirrorlist=|g" /etc/yum.repos.d/CentOS-*.repo
    echo "mirrorlist commented out."

    # 修改 CentOS 仓库配置文件，替换 baseurl 为阿里云镜像
    echo "Updating CentOS repository baseurl to Aliyun mirror..."
    sudo sed -i -e "s|^#baseurl=http://mirror.centos.org/\$contentdir/\$releasever|baseurl=https://mirrors.aliyun.com/centos-vault/\$contentdir/\$releasever|g" /etc/yum.repos.d/CentOS-*.repo
    echo "baseurl updated to Aliyun mirror."
else
    echo "Operation cancelled by user."
fi